package edu.hm.risikomanagement;

import edu.hm.risikomanagement.library.DatabaseData;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Einstellungen extends Activity {

	public static final String PREFS_NAME = "Settings";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.einstellungen);
		final Button btn_EinstellungenSave = (Button) findViewById(R.id.btn_EinstellungenSave);
		final Spinner spinnerBenutzer = (Spinner) findViewById(R.id.spinnerBenutzer);

		// BEGIN spinnerBenutzer
		String spinnerSelection = DatabaseData
				.getDataFromDatabase("risikoVerantwortliche");
		String[] itemsVerantwortlicher = spinnerSelection.split("@@@");
		ArrayAdapter<String> adapterVerantwortlicher = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_item,
				itemsVerantwortlicher);
		adapterVerantwortlicher
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerBenutzer.setAdapter(adapterVerantwortlicher);

		SharedPreferences preferences = getSharedPreferences(PREFS_NAME, 0);
		spinnerBenutzer.setSelection(preferences.getInt("benutzer", 0));
		// END spinnerBenutzer

		btn_EinstellungenSave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				SharedPreferences preferences = getSharedPreferences(
						PREFS_NAME, 0);
				SharedPreferences.Editor editor = preferences.edit();
				editor.putInt("benutzer",
						spinnerBenutzer.getSelectedItemPosition());
				editor.commit();

				finish();
			}
		});

	}
}